package com.itcam.controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

//6.演示使用ServletAPI
//7.演示使用保存作用域：request,session,application
@RestController
@RequestMapping("/hello06")
public class Hello06Controller {

    //1) HttpServletRequest , HttpServletResponse , HttpSession
    @GetMapping("/h01")
    public String h01(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("request = " + request);
        System.out.println("response = " + response);
        System.out.println("session = " + session);
        return "succ";
    }

    @Autowired
    private ServletContext servletContext;

    //2) ServletContext
    @GetMapping("/h02")
    public String h02(HttpServletRequest request, HttpSession session) {
        //方法1:
        //ServletContext servletContext = request.getServletContext();
        //方法2:
        //ServletContext servletContext = session.getServletContext();
        //方法3:
        System.out.println(servletContext);

        return "succ";
    }

    //3.演示向request、session和servletContext作用域保存数据
    @GetMapping("/h03")
    public String h03(HttpServletRequest request) {
        request.setAttribute("uname", "jim");
        request.getSession().setAttribute("age", 20);
        servletContext.setAttribute("address", "USA");

        return "succ";
    }

    //4.演示从request、session和servletContext作用域获取数据
    @GetMapping("/h04")
    public String h04(HttpServletRequest request) {
        System.out.println("request.getAttribute(\"uname\") = " + request.getAttribute("uname"));
        System.out.println("request.getSession().getAttribute(\"age\") = " + request.getSession().getAttribute("age"));
        System.out.println("servletContext.getAttribute(\"address\") = " + servletContext.getAttribute("address"));

        return "succ";
    }

    //5.演示通过springmvc提供的组件向request作用域保存数据
    @GetMapping("/h05")
    public String h05(Model model, ModelMap modelMap, Map map) {
        model.addAttribute("country", "China");
        System.out.println(model.getAttribute("country"));

        // 转发
        //request.getRequestDispatcher("").forward(request,response);
        // 重定向
        //response.sendRedirect("");

        modelMap.addAttribute("age", 99);
        System.out.println("modelMap.getAttribute(\"age\") = " + modelMap.getAttribute("age"));

        return "succ";
        //return "forward:page02";
        //return "redirect:page02";
    }

    @GetMapping("/h06")
    public ModelAndView h06(Model model, ModelMap modelMap, Map map) {
        ModelAndView mv = new ModelAndView();

        mv.addObject("hello", "world");
        mv.setViewName("page02");

        return mv;

    }

}
